const { idCreator } = require("../util/crypto");
const { query } = require("../util/mysql");

// 查找用户名
const getUserByName = async function (username) {
  const sql = `select uid from userinfo where username=?`;
  const params = [username];
  return await query(sql, params);
};
// 注册 传入 username, password  自动生成 uid
const registry = async function ({  username, password }) {
  const sql = `insert into userinfo (username,password) values (?,?)`;
  params = [ username, password];
  console.log(sql);
  console.log(username, password);
  return await query(sql,params);
};

const login = async function ({ username, password }) {
  const sql = `select username from userInfo where username=? and password=?`;
  const params = [username, password];
  return await query(sql, params);
};

const add = async ({username,password}) =>{

  const uid = idCreator(username);
  const sql = `insert into userinfo (uid,username,password) values (?,?,?)`
  const params = [uid,username,password]
  return await query(sql,params)
}
const del = async ({uid}) =>{
  const sql = `delete from userinfo where uid='${uid}'`
  return await query(sql)
};
const edit = async ({uid,username,password}) =>{
  console.log(uid,username,password,"-----");
  const sql = `update userinfo set username=?,password=? where uid=?`;
  const params = [username,password,uid];
  console.log(params);
  return await query(sql,params)
};
// const gets = async (username) =>{
//   const sql = `select uid from userinfo where username=?`;
//   const params = [username]
//   return await query(sql,params)
// };

const gets = async () =>{
  const sql = `select * from userinfo`;
  return await query(sql)
};
module.exports.userService = {
  getUserByName,
  registry,
  login,
  add,
  del,
  edit,
  gets
};
